package com.lxyk.service.monthly.impl;

import com.lxyk.dao.monthly.CourseInfoDao;
import com.lxyk.domain.CourseInfo;
import com.lxyk.service.monthly.CourseInfoService;
import com.lxyk.vo.CourseInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CourseInfoServiceImpl implements CourseInfoService {

    @Autowired
    private CourseInfoDao courseInfoDao;

    //根据班级id查询班级课程信息
    @Override
    public List<CourseInfo> findAllByCid(Integer cid) {
        return courseInfoDao.findByCid(cid);
    }

    //根据id修改上课时间
    @Override
    public Integer editDateById(Integer id, String date) {

        return courseInfoDao.editDateById(id,date);
    }

    //删除课程详情
    @Override
    public void deleteById(Integer id,Integer cid) {
        //查询班级课表
        List<CourseInfo> list = courseInfoDao.findByCid(cid);

        //将后面的课程排序-1
        boolean flag = false;
        for (CourseInfo courseInfo : list) {
            if (id.equals(courseInfo.getId())){
                flag = true;
            }
            if(flag){
                courseInfo.setSort(courseInfo.getSort()-1);
                courseInfoDao.editSortById(courseInfo);
            }
        }
        //删除
        courseInfoDao.deleteById(id);
    }

    //添加课程详情
    @Override
    public void addCourseInfo(CourseInfo courseInfo) {
        //查询班级课表
        List<CourseInfo> list = courseInfoDao.findByCid(courseInfo.getCid());
        //修改之前的课程详情排序,并更新
        for (CourseInfo info : list) {
            if(info.getSort()>=courseInfo.getSort()){
                info.setSort(info.getSort()+1);
                courseInfoDao.editSortById(info);
            }
        }
        //插入新增的课程详情数据
        courseInfo.setUseFlag(0);
        courseInfoDao.add(courseInfo);
    }

    //查询每个在读班级下节课上课时间
    @Override
    public List<CourseInfoVo> findCourseInfoTime() {
        return courseInfoDao.findCourseInfoTime();
    }

    @Override
    public List<CourseInfoVo> findNowCourseInfo() {
        return courseInfoDao.findNowCourseInfo();
    }

    //修改所有上课时间小于当前时间,且未上课的课程信息,改为上课状态
    @Override
    public void updateUseFlag(Date nowDate) {
        System.out.println("修改所有上课时间小于当前时间,且未上课的课程信息,改为上课状态");
        courseInfoDao.updateUseFlag(nowDate);
    }

    @Override
    public Integer findInfoCount(Integer cid) {
        return courseInfoDao.findInfoCount(cid);
    }

}
